package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.entiy.local.*;
import com.edt.ichannel.api.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CommissionRootRuleServiceImpl implements CommissionRootRuleService {

    @Resource
    private CommissionRuleService commissionRuleService;
    @Resource
    private MContractorCommissionRuleService mContractorCommissionRuleService;
    @Resource
    private MSignerCommissionRuleService mSignerCommissionRuleService;
    @Resource
    private MQyCommissionRuleService mQyCommissionRuleService;
    @Resource
    private BfCommissionRuleService bfCommissionRuleService;
    @Resource
    private BfContractCommissionRuleService bfContractCommissionRuleService;
    @Resource
    private BfSignerCommissionRuleService bfSignerCommissionRuleService;
    @Resource
    private BfQyCommissionRuleService bfQyCommissionRuleService;

    @Resource
    private ProductService productService;
    @Resource
    private BalanceProductService balanceProductService;
    @Resource
    private OrderChannelService orderChannelService;

    @Resource
    private XsCommissionRuleService xsCommissionRuleService;
    @Resource
    private XsBfCommissionRuleService xsBfCommissionRuleService;

    @Resource
    private CommissionMonthRuleService commissionMonthRuleService;
    @Resource
    private CommissionMonthRuleLinkService commissionMonthRuleLinkService;

    @Override
    public CommissionRuleRoot getCommissionRuleCK(String cityCode, String productCode, int firstMoney, int type, Date firstDate) {
        if(IceStringUtils.isAnyBlank(cityCode,productCode)){
            return null;
        }
        String productId = productService.getProductIdByCode(productCode);
        if(balanceProductService.isBalanceProduce(productId)){
            CommissionRuleRoot rootRule;
            switch (type){
                case CommonConstant.STAFF_TYPE_ZY:
                    CommissionRule commissionRule = commissionRuleService.getCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(commissionRule == null){
                        return null;
                    }
                    rootRule = new CommissionRuleRoot();
                    BeanUtils.copyProperties(commissionRule,rootRule);
                    break;
                case CommonConstant.STAFF_TYPE_CB:
                    MContractorCommissionRule mContractorCommissionRule = mContractorCommissionRuleService.getCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(mContractorCommissionRule == null){
                        return null;
                    }
                    rootRule = new CommissionRuleRoot();
                    BeanUtils.copyProperties(mContractorCommissionRule,rootRule);
                    break;
                case CommonConstant.STAFF_TYPE_QIANY:
                    MSignerCommissionRule mSignerCommissionRule = mSignerCommissionRuleService.getCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(mSignerCommissionRule == null){
                        return null;
                    }
                    rootRule = new CommissionRuleRoot();
                    BeanUtils.copyProperties(mSignerCommissionRule,rootRule);
                    break;
                default:
                    MQyCommissionRule mQyCommissionRule = mQyCommissionRuleService.getCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(mQyCommissionRule == null){
                        return null;
                    }
                    rootRule = new CommissionRuleRoot();
                    BeanUtils.copyProperties(mQyCommissionRule,rootRule);
                    break;
            }
            return rootRule;
        }
        return null;
    }


    @Override
    public CommissionRuleRoot getCommissionRuleKD(String cityCode, String productCode, int firstMoney, int type, Date firstDate){

        if(IceStringUtils.isAnyBlank(cityCode,productCode)){
            return null;
        }
        String productId = productService.getProductIdByCode(productCode);
        if(balanceProductService.isBalanceProduce(productId)){
            CommissionRuleRoot rootRule = new CommissionRuleRoot();
            switch (type){
                case CommonConstant.STAFF_TYPE_ZY:
                    BfCommissionRule commissionRule = bfCommissionRuleService.getBfCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(commissionRule == null){
                        return null;
                    }
                    BeanUtils.copyProperties(commissionRule,rootRule);
                    break;
                case CommonConstant.STAFF_TYPE_CB:
                    BfContractCommissionRule mContractorCommissionRule = bfContractCommissionRuleService.getBfContractCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(mContractorCommissionRule == null){
                        return null;
                    }
                    BeanUtils.copyProperties(mContractorCommissionRule,rootRule);
                    break;
                case CommonConstant.STAFF_TYPE_QIANY:
                    BfSignerCommissionRule mSignerCommissionRule = bfSignerCommissionRuleService.getBfSignerCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(mSignerCommissionRule == null){
                        return null;
                    }
                    BeanUtils.copyProperties(mSignerCommissionRule,rootRule);
                    break;
                default:
                    BfQyCommissionRule mQyCommissionRule = bfQyCommissionRuleService.getBfQyCommissionRule(productId,cityCode,firstMoney,firstDate);
                    if(mQyCommissionRule == null){
                        return null;
                    }
                    BeanUtils.copyProperties(mQyCommissionRule,rootRule);
                    break;
            }

            return rootRule;
        }
       return null;
    }

    @Override
    public boolean matchCommisionRule(OrderCreditKd orderCreditKd) {
        if(orderCreditKd.getChannelType() == null){
            orderChannelService.setExpansionTypeAndLevl(orderCreditKd);
        }
        CommissionRuleRoot commissionRuleRoot = getCommissionRuleCK(orderCreditKd.getCityCode(),orderCreditKd.getProductCode(),orderCreditKd.getFirstMoney(),orderCreditKd.getChannelType(),orderCreditKd.getFirstDate());
        if(commissionRuleRoot == null){

            CommissionMonthRule commissionMonthRule = getCommissionMonthRule(orderCreditKd.getCityCode(),orderCreditKd.getProductCode(),orderCreditKd.getFirstMoney(),orderCreditKd.getChannelType(),orderCreditKd.getFirstDate());
            return commissionMonthRule != null;
        }
        return true;
    }

    @Override
    public boolean matchCommisionRuleKdC(OrderCreditKd orderCreditKd) {
        if(orderCreditKd.getChannelType() == null){
            orderChannelService.setExpansionTypeAndLevl(orderCreditKd);
        }
        CommissionRuleRoot commissionRuleRoot = getCommissionRuleKDC(orderCreditKd.getCityCode(),orderCreditKd.getProductCode(),orderCreditKd.getFirstMoney(),orderCreditKd.getFirstDate());
        return commissionRuleRoot!= null;
    }

    @Override
    public CommissionMonthRule getCommissionMonthRule(String cityCode, String productCode, Integer firstMoney, Integer channelType, Date firstDate) {
        //查询按月发放规则
        String productId = productService.getProductIdByCode(productCode);
        if(balanceProductService.isBalanceProduce(productId)){
            return commissionMonthRuleService.getCommissionMonthRule(cityCode,productId,firstMoney,channelType,firstDate);
        }

        return null;
    }

    @Override
    public CommissionMonthRuleLink getNextCommissionRuleMonthLink(String id, int nextCount) {
        return commissionMonthRuleLinkService.getNextCommissionRuleMonthLink(id,nextCount);
    }

    @Override
    public CommissionMonthRule getCommissionMonthRuleById(String ruleId) {

        return commissionMonthRuleService.getCommissionMonthRuleById(ruleId);
    }

    @Override
    public boolean matchCommisionRuleKd(OrderCreditKd orderCreditKd) {
        if(orderCreditKd.getChannelType() == null){
            orderChannelService.setExpansionTypeAndLevl(orderCreditKd);
        }
        CommissionRuleRoot commissionRuleRoot = getCommissionRuleKD(orderCreditKd.getCityCode(),orderCreditKd.getProductCode(),orderCreditKd.getPhoneFirstMoney(),orderCreditKd.getChannelType(),orderCreditKd.getFirstDate());
        if(commissionRuleRoot == null){

            CommissionMonthRule commissionMonthRule = getCommissionMonthRule(orderCreditKd.getCityCode(),orderCreditKd.getProductCode(),orderCreditKd.getFirstMoney(),orderCreditKd.getChannelType(),orderCreditKd.getFirstDate());
            return commissionMonthRule != null;
        }
        return true;
    }

    @Override
    public boolean matchCommisionRuleC(OrderCreditKd orderCreditKd) {
        CommissionRuleRoot commissionRuleRoot = getCommissionRuleCKC(orderCreditKd.getCityCode(),orderCreditKd.getProductCode(),orderCreditKd.getFirstMoney(),orderCreditKd.getFirstDate());
        return commissionRuleRoot!= null;
    }

    @Override
    public CommissionRuleRoot getCommissionRuleCKC(String cityCode, String productCode, Integer firstMoney, Date firstDate) {

        if(IceStringUtils.isAnyBlank(cityCode,productCode)){
            return null;
        }
        String productId = productService.getProductIdByCode(productCode);
        if(balanceProductService.isBalanceProduce(productId)){
            XsCommissionRule xsCommissionRule = xsCommissionRuleService.getXsCommissionRule(productId,cityCode,firstMoney,firstDate);
            if(xsCommissionRule == null){
                return null;
            }
            CommissionRuleRoot rootRule = new CommissionRuleRoot();
            BeanUtils.copyProperties(xsCommissionRule,rootRule);
            return rootRule;
        }
        return null;

    }

    @Override
    public CommissionRuleRoot getCommissionRuleKDC(String cityCode, String productCode, Integer firstMoney, Date firstDate) {
        if(IceStringUtils.isAnyBlank(cityCode,productCode)){
            return null;
        }
        String productId = productService.getProductIdByCode(productCode);
        if(balanceProductService.isBalanceProduce(productId)){
            XsBfCommissionRule xsBfCommissionRule = xsBfCommissionRuleService.getXsBfCommissionRule(productId,cityCode,firstMoney,firstDate);
            if(xsBfCommissionRule == null){
                return null;
            }
            CommissionRuleRoot rootRule = new CommissionRuleRoot();
            BeanUtils.copyProperties(xsBfCommissionRule,rootRule);
            return rootRule;
        }
        return null;
    }


}
